﻿using UnityEditor;
using UnityEngine;
using System.IO;

namespace com.yoozoo.gta.Art.Editor
{
    public class LODMaterialReplacer : EditorWindow
    {
        private string directoryPath = "Assets/YourDirectory"; // 指定目录
        private Material oldMaterial; // 旧的材质球
        private Material newMaterial; // 新的材质球

        [MenuItem("HRP/LOD Material Replacer")]
        public static void ShowWindow()
        {
            GetWindow<LODMaterialReplacer>("LOD Material Replacer");
        }

        private void OnGUI()
        {
            GUILayout.Label("Replace LOD1 Material in Prefabs", EditorStyles.boldLabel);

            directoryPath = EditorGUILayout.TextField("Directory Path", directoryPath);
            oldMaterial = (Material)EditorGUILayout.ObjectField("Old Material", oldMaterial, typeof(Material), false);
            newMaterial = (Material)EditorGUILayout.ObjectField("New Material", newMaterial, typeof(Material), false);

            if (GUILayout.Button("Replace Materials"))
            {
                ReplaceMaterialsInPrefabs();
            }
        }

        private void ReplaceMaterialsInPrefabs()
        {
            if (string.IsNullOrEmpty(directoryPath) || oldMaterial == null || newMaterial == null)
            {
                Debug.LogError("Please specify the directory path and materials.");
                return;
            }

            string[] prefabFiles = Directory.GetFiles(directoryPath, "*.prefab", SearchOption.AllDirectories);
            foreach (string prefabFile in prefabFiles)
            {
                string assetPath = prefabFile.Replace("\\", "/");
                GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(assetPath);
                if (prefab != null)
                {
                    bool changed = false;

                    LODGroup[] lodGroups = prefab.GetComponentsInChildren<LODGroup>();
                    foreach (LODGroup lodGroup in lodGroups)
                    {
                        if (lodGroup == null)
                        {
                            Debug.LogWarning($"LODGroup is null in prefab: {assetPath}");
                            continue;
                        }

                        LOD[] lods = lodGroup.GetLODs();
                        if (lods.Length > 1 && lods[1].renderers != null) // 确保LOD1存在
                        {
                            Renderer[] renderers = lods[1].renderers;
                            foreach (Renderer renderer in renderers)
                            {
                                if (renderer == null)
                                {
                                    Debug.LogWarning($"Renderer is null in LODGroup of prefab: {assetPath}");
                                    continue;
                                }

                                Material[] materials = renderer.sharedMaterials;
                                if (materials != null)
                                {
                                    for (int i = 0; i < materials.Length; i++)
                                    {
                                        if (materials[i] == oldMaterial)
                                        {
                                            materials[i] = newMaterial;
                                            changed = true;
                                        }
                                    }

                                    if (changed)
                                    {
                                        renderer.sharedMaterials = materials;
                                    }
                                }
                                else
                                {
                                    Debug.LogWarning($"Materials array is null in renderer of prefab: {assetPath}");
                                }
                            }
                        }
                        else
                        {
                            Debug.LogWarning($"LOD1 does not exist or has no renderers in prefab: {assetPath}");
                        }
                    }

                    if (changed)
                    {
                        PrefabUtility.SaveAsPrefabAsset(prefab, assetPath);
                        Debug.Log($"Updated prefab: {assetPath}");
                    }
                }
                else
                {
                    Debug.LogWarning($"Could not load prefab at path: {assetPath}");
                }
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            Debug.Log("Material replacement completed.");
        }
    }
}